package com.example.cinema.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.cinema.R;
import com.example.cinema.UserAttribute;
import com.example.cinema.databinding.ActivityUserpostBinding;
import com.example.cinema.request.RequestSend;

import java.sql.Struct;
import java.util.Timer;
import java.util.TimerTask;

public class ActivityUserpost extends AppCompatActivity {

    private ActivityUserpostBinding APB;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        APB = DataBindingUtil.setContentView(this, R.layout.activity_userpost);
        init();
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }
    }

    private UserAttribute attribute;
    private RequestSend requestSend;
    private boolean sendstate;

    private void init() {
        requestSend = new RequestSend(getApplicationContext());
        attribute = (UserAttribute) getApplication();
        //返回
        APB.finsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //发送验证
        APB.sendcode.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!sendstate) {
                    sendstate = true;

                    String phpne = APB.phone.getText().toString();
                    String pswds = APB.pswds.getText().toString();
                    if (phpne.length()<11) {
                        Toast.makeText(getApplicationContext(), "手机号码错误", Toast.LENGTH_LONG).show();
                        sendstate = false;
                        return;
                    }
                    if(pswds.length()<6){
                        Toast.makeText(getApplicationContext(), "密码不能少于6位", Toast.LENGTH_LONG).show();
                        sendstate = false;
                        return;
                    }
                    SendCodes(phpne);
                    //进行发送短信
                    Timers();
                }
            }
        });
        //注册发送
        APB.confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                String id = manager.getDeviceId();

                String phpne = APB.phone.getText().toString();
                String pswds = APB.pswds.getText().toString();
                String codes = APB.codeeidt.getText().toString();
                if (phpne.length() < 11) {
                    Toast.makeText(getApplicationContext(), "手机号码错误", Toast.LENGTH_LONG).show();
                    APB.confirm.setEnabled(true);
                    return;
                }
                if (pswds.length() < 6) {
                    Toast.makeText(getApplicationContext(), "密码不少于6位", Toast.LENGTH_LONG).show();
                    APB.confirm.setEnabled(true);
                    return;
                }
                //发送修改
                SendRequest(phpne,pswds, codes,id);
                APB.confirm.setEnabled(false);
            }
        });
    }


    private int times = 60;

    private void Timers() {
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                times--;
                Message message = new Message();
                if (times <= 0) {
                    times = 60;
                    sendstate = false;
                    message.what = 1001;
                    message.obj = "发送验证";
                    handler.sendMessage(message);
                    timer.cancel();
                    return;
                }

                message.what = 1001;
                message.obj = String.valueOf(times);
                handler.sendMessage(message);
            }
        };
        timer.schedule(timerTask, 1000, 1000);
    }


    //发送注册
    private void SendRequest(String phone,String pwd, String code,String id) {
        new Thread() {
            @Override
            public void run() {
                boolean bool = requestSend.ReqUserPost(phone,pwd,code,id);

                Message message = new Message();
                message.what = 1000;
                message.obj = bool;
                handler.sendMessage(message);
            }
        }.start();
    }

    //发送验证码
    private void SendCodes(String phone) {
        new Thread() {
            @Override
            public void run() {
                boolean bool = requestSend.SendPostCode(phone);
                Message message = new Message();
                message.what = 1002;
                message.obj = bool;
                handler.sendMessage(message);
            }
        }.start();
    }

    //UI线程
    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1000) {
                if ((boolean) msg.obj) {
                    SuccessToast();
                    APB.confirm.setEnabled(true);
                    return;
                } else {
                    Toast.makeText(getApplicationContext(), "注册失败验证码错误", Toast.LENGTH_LONG).show();
                    APB.confirm.setEnabled(true);
                }
                return;
            }
            if (msg.what == 1001) {
                String ss = (String) msg.obj;
                APB.sendcode.setText(ss);
                return;
            }
            if (msg.what == 1002) {
                boolean bol = (boolean) msg.obj;
                if (bol) {
                    Toast.makeText(getApplicationContext(), "验证码已发送", Toast.LENGTH_LONG).show();
                    return;
                } else {
                    Toast.makeText(getApplicationContext(), "验证码发送失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    };

    private Toast toast;

    private void SuccessToast() {
        TextView textView = new TextView(getApplicationContext());
        ImageView imageView = new ImageView(getApplicationContext());
        textView.setText("注册成功！");
        textView.setTextColor(Color.WHITE);
        imageView.setImageResource(R.mipmap.success);


        LinearLayout linearLayout = new LinearLayout(getApplicationContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.setLayoutParams(params);
        linearLayout.addView(imageView);
        linearLayout.addView(textView);



        toast = new Toast(getApplicationContext());
        toast.setView(linearLayout);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }
}
